package com.ganzalang.gmall.rocketmq.producer;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

/**
 * @description: 订单消息生产者
 * @author: Ganzalang
 * @Date 2021/5/8 11:36
 */
@Component
@Slf4j
public class OrderProducer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public void send() {
        // 发送消息
        rocketMQTemplate.convertAndSend("Topic1:TagA", "发送普通消息");
        log.info("发送普通消息成功");

        // 发送spring的Message
        rocketMQTemplate.send("Topic1:TagA", MessageBuilder.withPayload("发送Spring的Message").build());
        log.info("发送spring的message成功");

        // 发送异步消息
        rocketMQTemplate.asyncSend("Topic1:TagA", "发送异步消息", new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                log.info("发送异步消息成功");
            }

            @Override
            public void onException(Throwable throwable) {
                log.info("发送异步消息失败");
            }
        });

        // 发送顺序消息
        rocketMQTemplate.syncSendOrderly("Topic1", "发送顺序消息1", "123");
        log.info("发送顺序消息1成功");
        rocketMQTemplate.syncSendOrderly("Topic1", "发送顺序消息2", "124");
        log.info("发送顺序消息2成功");
        rocketMQTemplate.syncSendOrderly("Topic1", "发送顺序消息3", "125");
        log.info("发送顺序消息3成功");
    }
}
